package com.codeoss.framework.view.pagebean;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Container;
import com.codeoss.framework.view.component.IDataBinding;
import com.codeoss.framework.view.component.IFormFieldExpression;
import com.codeoss.framework.view.component.IVarName;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.chart.IChart;
import com.codeoss.framework.view.component.report.UReport2;
import com.codeoss.framework.view.pagebean.base.BasePageBean;
import com.codeoss.framework.view.service.IChartComponentService;
import com.codeoss.framework.view.service.IUReport2ComponentService;
import com.codeoss.framework.view.service.IVariableService;
import com.codeoss.framework.view.service.store.IComponentServiceStore;
import com.codeoss.framework.view.util.ComponentDataUtil;
import com.codeoss.framework.view.util.PageBeanUtil;

@Service
public class DashboardPageBean extends BasePageBean {
	@Autowired
	private IUReport2ComponentService uReport2ComponentService;
	@Autowired
	private PageBeanUtil pageBeanUtil;
	@Value("${framework.db-type}")
	private String dbType;
	@Autowired
	private IComponentServiceStore componentServiceStore;
	@Autowired
	private IVariableService variableService;
	@Autowired
	private ComponentDataUtil componentDataUtil;
	public static final String KEY_SEARCH_FIELD_LIST = "key_search_field_list";
	public static final String KEY_CHART_LIST = "key_chart_list";
	public static final String KEY_UREPORT2_LIST = "key_ureport2_list";
	public void beforeInit() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		if (page.isInit()) {
			return;
		}
		List<IDataBinding> searchDataBindingList = new ArrayList<IDataBinding>();
		pageBeanUtil.getFormFieldList(page, searchDataBindingList);
		page.setAttribute(KEY_SEARCH_FIELD_LIST, searchDataBindingList);
		
		List<IChart> chartList = new ArrayList<IChart>();
		getChartList(page, chartList);
		page.setAttribute(KEY_CHART_LIST, chartList);
		
		pageBeanUtil.getComponentList(yunContext, page, "ureport2");
		List<UReport2> uReport2List = yunContext.getAttribute("__componentList__");
		page.setAttribute(KEY_UREPORT2_LIST, uReport2List);
		
		page.setInit(true);
	}
	
	public void init() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		List<IChart> chartList = page.getAttribute(KEY_CHART_LIST);
		for (int i = 0; i < chartList.size(); i++) {
			  IChart chart = chartList.get(i);
			  IChartComponentService chartComponentService=componentServiceStore.getComponentService(yunContext, (Component)chart);
			  chartComponentService.getChartData(yunContext, (Component)chart);
		}
		List<UReport2> uReport2List = page.getAttribute(KEY_UREPORT2_LIST);
		if (uReport2List != null) {
			for (int i = 0; i < uReport2List.size(); i++) {
				UReport2 uReport2 = uReport2List.get(i);
				uReport2ComponentService.dataQuery(yunContext, uReport2, yunContext.getVarValueMap());
			}
		}
		
		List<IDataBinding> searchDataBindingList=page.getAttribute(KEY_SEARCH_FIELD_LIST);
		for(int i=0;i<searchDataBindingList.size();i++) {
			IDataBinding dataBinding = searchDataBindingList.get(i);
			if(dataBinding instanceof IFormFieldExpression) {
				Object  defaultValue=componentDataUtil.getDefaultValue(yunContext, page, (IFormFieldExpression)dataBinding);
				variableService.setVariableValue(yunContext,((IVarName)dataBinding).getVarName(), defaultValue);
			}else {
				variableService.setVariableValue(yunContext,((IVarName)dataBinding).getVarName(), null);
			}
		}
		pageBeanUtil.generateFormInfo(searchDataBindingList);
	}

	protected void getChartList(Component component, List<IChart> chartDataList) {
		if (component instanceof IChart) {
			chartDataList.add((IChart) component);
		}
		if (component instanceof Container) {
			Container c = (Container) component;
			for (int i = 0; i < c.getChildren().size(); i++) {
				getChartList(c.getChildren().get(i), chartDataList);
			}
		}
	}
	
	public void dataSearch() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		List<IChart> chartList = page.getAttribute(KEY_CHART_LIST);
		for (int i = 0; i < chartList.size(); i++) {
			  IChart chart = chartList.get(i);
			  IChartComponentService chartComponentService=componentServiceStore.getComponentService(yunContext, (Component)chart);
			  chartComponentService.getChartData(yunContext, (Component)chart);
		}
		List<UReport2> uReport2List = page.getAttribute(KEY_UREPORT2_LIST);
		if (uReport2List != null) {
			for (int i = 0; i < uReport2List.size(); i++) {
				UReport2 uReport2 = uReport2List.get(i);
				uReport2ComponentService.dataQuery(yunContext, uReport2, yunContext.getVarValueMap());
			}
		}
	}
}
